package com.lijian.user.controller;

import com.lijian.common.domain.R;
import com.lijian.user.domain.dto.LoginFormByEmailDTO;
import com.lijian.user.domain.dto.LoginFormDTO;
import com.lijian.user.domain.dto.RegisterFormDTO;
import com.lijian.user.domain.vo.UserLoginVO;
import com.lijian.user.service.CaptchaService;
import com.lijian.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;

/**
 * @author LiJian
 * @date 2024-10-22
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;
    private final CaptchaService captchaService;

    @ApiOperation("用户登录接口")
    @PostMapping("login")
    public UserLoginVO login(@ApiParam("登录表单") @RequestBody @Validated LoginFormDTO loginFormDTO) {
        return userService.login(loginFormDTO);
    }

    @ApiOperation("用户邮箱登录接口")
    @PostMapping("loginByEmail")
    public UserLoginVO loginByEmail(@ApiParam("邮箱登录表单") @RequestBody @Validated LoginFormByEmailDTO loginFormByEmailDTO) {
        return userService.loginByEmail(loginFormByEmailDTO);
    }

//    @ApiOperation("用户注册")
//    @PostMapping("register")
//    public R<?> register(@RequestBody @Validated RegisterFormDTO registerFormDTO){
//        return userService.register(registerFormDTO);
//    }

    @ApiOperation("发送验证码")
    @PostMapping("getCaptcha")
    public R<?> getCaptcha(@PathParam("phone") String phone) {
        if (captchaService.getSmsCaptcha(phone)) {
            return R.ok("发送验证码成功");
        } else {
            return R.error("发送验证码失败");
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("editPassword")
    public R<?> editPassword(@RequestBody @Validated LoginFormByEmailDTO loginFormByEmailDTO) {
        return userService.editPassword(loginFormByEmailDTO);
    }
}
